package com.xinyiweidai.xinyiweidai.dialog;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.xinyiweidai.xinyiweidai.R;


/**
 * class:   UpdateDialog
 * data：   2018/9/5  14:20
 * author:  LanX
 * Description:
 *
 */

public class UpdateDialog extends DialogFragment {


    private TextView tv_update,tv_cancel;
    private OnUpdateDialogClickListener onUpdateDialogClickListener;


    public interface OnUpdateDialogClickListener {
        void onUpdateDialogClick();
    }

    private void setOnUpdateDialogClickListener(OnUpdateDialogClickListener onUpdateDialogClickListener) {
        this.onUpdateDialogClickListener = onUpdateDialogClickListener;
    }


    public static UpdateDialog newInstance(String packageSize, int enforce, OnUpdateDialogClickListener onUpdateDialogClickListener) {
        Bundle args = new Bundle();
        args.putString("packageSize",packageSize);
        args.putInt("enforce",enforce);
        UpdateDialog fragment = new UpdateDialog();
        fragment.setArguments(args);
        fragment.setOnUpdateDialogClickListener(onUpdateDialogClickListener);
        return fragment;
    }


    @SuppressLint("ClickableViewAccessibility")
    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.CENTER;
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(params);
        View decorView = window.getDecorView();
        decorView.setPadding(100, 0, 100, 0);
        decorView.setBackground(new ColorDrawable(Color.TRANSPARENT));
        decorView.setOnTouchListener((v, event) -> {
            if (event.getAction() == MotionEvent.ACTION_UP) {}
            return true;
        });
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.dialog_update, container, false);
        tv_update=rootView.findViewById(R.id.tv_update);
        tv_cancel=rootView.findViewById(R.id.tv_cancel);
        tv_cancel.setVisibility((int)getArguments().get("enforce")==1?View.GONE:View.VISIBLE);
        double size = Double.parseDouble(String.valueOf(getArguments().get("packageSize")));
        tv_update.setText("更新 ("+size+"MB)");
        addClickEvent();
        return rootView;
    }


    private void addClickEvent() {
        tv_update.setOnClickListener(view -> {
            if (onUpdateDialogClickListener!=null){
                onUpdateDialogClickListener.onUpdateDialogClick();
            }
            UpdateDialog.this.dismiss();
        });
        tv_cancel.setOnClickListener(view -> UpdateDialog.this.dismiss());
    }
}
